package com.liu.webstore.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author LiuYumei
 * @Date 2022/7/23 9:44
 */
@Component
public class DemoInterceptor implements HandlerInterceptor {
    private static Logger log = LoggerFactory.getLogger(DemoInterceptor.class);
    /**
     * 在controller代码处理请求之前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("preHandle: " + handler.toString());
        return HandlerInterceptor.super.preHandle(request, response, handler);
        //return true;
    }

    /**
     * 在controller代码处理请求之后执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("postHandle: " + handler.toString());
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 在TemplateEngine之后执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.debug("afterCompletion: " + handler.toString());
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
